package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws ParseException {
        //1.判断预约的日期是否进行过预约设置，即确定当天是否可预约
        String orderDate = (String) map.get("orderDate");
        System.out.println(orderDate);
        if (orderDate == null){
            return new Result(false,"预约日期不能为空");
        }
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
        //因为后续要判断是否预约满，就需要取其中的数据，所以重新构造一个方法
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByDate(date);
        if(orderSetting == null){
            //当天没进行预约设置，不可预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.判断当天是否已经预约满了
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            //如果预约数大于等于可预约数，预约已满，不能预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3.判断是否重复预约
        String telephone = (String) map.get("telephone");
        Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
        //根据手机号，获取member_id
        Member member = memberDao.findByTelephone(telephone);

        //4.判断当前手机号是否存在于member表中，即是否为成为用户
        //如果没有成员用户，就自动成为用户
        if(member != null){
            //如果是此手机号已成为用户
            Integer memberId = member.getId();
            //根据用户的id，预约日期，套餐id从order表中判断是否重复预约
            Order order = new Order();
            order.setMemberId(memberId);
            order.setOrderDate(date);
            order.setSetmealId(setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if(orderList != null && orderList.size() >0){
                //说明，当前用户，当前日期，当前套餐已经预约过
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else{
            //如果此手机号没有成为用户，就不会存在重复预约的情况
            //只需要自动给这个手机号码加入到用户
            //如果能走到这，说名member为null，重新赋值
            member = new Member();
            String name = (String) map.get("name");
            member.setName(name);
            String sex = (String) map.get("sex");
            member.setSex(sex);
            member.setPhoneNumber(telephone);
            String idCard = (String) map.get("idCard");
            member.setIdCard(idCard);
            member.setRegTime(new Date());
            memberDao.add(member);
        }


        //5.此时才真正预约，在order中插入该条数据
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String)map.get("orderType"));
        order.setSetmealId(setmealId);
        orderDao.add(order);

        //更新预约设置表中的预约数
        int reservations = orderSetting.getReservations() + 1;
        orderSetting.setReservations(reservations);
        orderSettingDao.updateReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }


    @Override
    public Map findById(Integer orderId) {
        Map map = orderDao.findById4Detail(orderId);
        //此时查询数据库中的日期为date类型数据，发送前台需要转为成2020-3-23类型
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            String date = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
            map.put("orderDate",date);
        }
        return map;
    }

}
